var jgzs = {
    setBtnStatus:function () {
        var row = $('#jgzs_grid').datagrid('getSelected');
        if (row && row.id) {
            $("#btn_jgzs_mod").show();
            $("#btn_jgzs_del").show();
        } else {
            $("#btn_jgzs_mod").hide();
            $("#btn_jgzs_del").hide();
        }
    }
}

$(function () {
    //表格初始加载
    $('#jgzs_grid').datagrid({
        url:$.webPath() +"/zjgl/jgzs/jgzsList",
        toolbar:"#toolbar_jgzs",
        pagination:true,
        rownumbers:true,
        fitColumns:true,
        pageNumber: 1,
        singleSelect:true,
        fit:true,
        striped:true,
        columns:[[
            {field: 'jgrq', title: '日期',  width: 50/*,formatter: function (value) {
             var val = formatDate(sysDate, value, 5);
             return val
             }*/},
            {field: 'pjzs', title: "平均指数", editor: {type: 'text'}, width: 50},
            {field: 'jzz', title: "基准值(%)", editor: {type: 'text'}, width: 50}
        ]],
        onLoadSuccess:jgzs.setBtnStatus,
        onClickRow:jgzs.setBtnStatus
       /* onDblClickCell:function (index, field) {
            if (field == "name") {
                jgzs_modify('修改', 'update_jgzs');
            }
        }*/
    });
    //**********按钮触发事件**********
    //新增按钮
    $("#btn_jgzs_add").click(function () {
        jgzs_add('新增', 'add_jgzs');
    });
    //修改按钮
    $("#btn_jgzs_mod").click(function () {
        jgzs_modify('修改', 'update_jgzs');
    });
    //删除按钮
    $("#btn_jgzs_del").click(function () {
        jgzs_delete();
    });
    //查询按钮
   /* $("#btn_jgzs_search").click(function () {
        jgzs_search();
    });*/

});


//新增
function jgzs_add(title, action) {
    var href = $.webPath() + "/zjgl/jgzs/jgzs_content";
    var actionUrl = $.webPath() + "/zjgl/jgzs/" + action;
    jgzs_showDialog(title, href, actionUrl);

}

//修改
function jgzs_modify(title, action) {
    var record = $("#jgzs_grid").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要更新的记录!", "info");
        return;
    }
    var href = $.webPath() + "/zjgl/jgzs/jgzs_content?jgzsId=" + record.id;
    var actionUrl = $.webPath() + "/zjgl/jgzs/" + action;
    jgzs_showDialog(title, href, actionUrl);
}

//弹出新增或修改的窗口
function jgzs_showDialog(title, href, actionUrl) {
    var win = $.window({
        onBeforeClose:function(){
            $(".validatebox-tip").remove();
        },
        title:title,
        width:520,
        height:300,
        cache:false,
        url:href,
        iconCls: 'icon-add',
        modal:true,
        buttons:[
            {
                text:'保存',
                handler:function () {
                    var valid = $('#form_jgzs').form('validate');

                    $(win).mask();

                    $('#form_jgzs').form('submit', {
                        url:actionUrl,
                        onSubmit:function () {
                        },
                        success:function (data) {
                            //$.messager.progress('close');
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#jgzs_grid').datagrid('reload');
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//删除
function jgzs_delete() {
    var row = $('#jgzs_grid').datagrid('getSelected');
    if (row) {
        $.messager.confirm('确认', '是否确认删除？', function (r) {
            if (r) {
                var index = $('#jgzs_grid').datagrid('getRowIndex', row);
                $.ajax({
                    url:$.webPath()+'/zjgl/jgzs/delete_jgzs',
                    dataType: 'json',
                    type: 'POST',
                    data: {jgzsId:row.id},
                    beforeSend: function (){
                        $("#toolbar_jgzs").mask();
                    },
                    success:function(data){
                        $("#toolbar_jgzs").mask("hide");
                        if(data.statusCode == 200){
                            $('#jgzs_grid').datagrid('deleteRow', index);
                            $.messager.show({
                                title:'提示',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        }
                    }
                });
            } else {
                $.messager.alert("提示", obj.message, "error");
            }
        });
    } else {
        $.messager.alert("提示", "请选择要删除！", "warning");
    }
}

/*//查询
function jgzs_search() {
    $('#jgzs_grid').datagrid('load',
        {
            name:$('#jgzs_name').val(),
            action:$('#jgzs_action').val()
        }
    );
}*/
